package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.util.EatShowSetting;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.CityAdapter;
import com.xgkj.diyiketang.activity.data.PaiHangAdatper;

import com.xgkj.diyiketang.data.AreaInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.CityListTask;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/3/22.
 */
public class CityListActivity extends BaseActivity implements PullToRefreshListView.OnRefreshListener,PullToRefreshListView.OnLoadDataListener {
    private Context mContext;
    private PullToRefreshListView mListView;
    private CityAdapter mAdapter;
    private int cityTaskId = -1;
    private int currentPage = 1;
    private int size = 10;
    private TextView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.city_list);
        mContext=this;
        initView();
    }

    private void initView(){
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
//        sousuo = (ImageView) view.findViewById(R.id.paihang_search);
//        sousuo.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                startActivity(new Intent(mContext, SearchAnchorActivity.class));
//            }
//        });
        mListView = (PullToRefreshListView)findViewById(R.id.citylist);
        mListView.init(mContext,true);
        mAdapter = new CityAdapter(mContext);
        mListView.setAdapter(mAdapter,true);
        mListView.setOnRefreshListener(this);
        mListView.setOnLoadDataListener(this);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(mListView.getHeaderViewsCount()>0){
                    position--;
                }
                if (position<0 || position>=mAdapter.getCount())return;
                AreaInfo info = (AreaInfo) mAdapter.getItem(position);
                Intent intent = new Intent();
//                EatShowSetting.getInstance(mContext).setLocal(info.getAreaName());
                intent.putExtra("areaName", info.getAreaName());
                intent.putExtra("areaId", info.getAreaId());

                //设置返回数据
                CityListActivity.this.setResult(RESULT_OK, intent);
                //关闭Activity
                CityListActivity.this.finish();
            }
        });
        mListView.doRefresh();
    }
    private void getCityList() {
        if(cityTaskId != -1){
            return;
        }
        if(currentPage == 0){
            mListView.setNoNextPage();
            return;
        }

        CityListTask task = new CityListTask();
        task.addListener(cityListenter);
        cityTaskId = getEatShowApp().getTaskManager().addTask(task);
    }


    private TaskEventListener cityListenter = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if(taskId == cityTaskId){
                cityTaskId = -1;
                if(currentPage == 1){
                    mAdapter.clearData();
                    mListView.onRefreshComplete();
                    mListView.enableLoadMore();
                }else{
                    mListView.onLoadDataComplete(true);
                }
                List<AreaInfo> fansList = new ArrayList<AreaInfo>();
                if(data != null){
                    fansList = (List<AreaInfo>) data;
                }

                if(fansList != null && fansList.size()>0){
                    mAdapter.updateData(fansList);
                }
//                mListView.setSelected(true);
//                mListView.setSelection(mAdapter.getCount()-1);

                currentPage = result;


            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if(taskId == cityTaskId){
                cityTaskId = -1;
                if(currentPage == 1){
                    mListView.onRefreshComplete();
                }else{
                    mListView.onLoadDataComplete(false);
                }
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }

    };


    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getCityList();
    }

    @Override
    public void onRefresh() {
    currentPage = 1;
        getCityList();
    }
}
